package com.example.demo.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import com.example.demo.util.dto.Data;
/**
 *
 * 响应数据包装工具类
 *
 * 增加国际化返回
 * 
 *
 */
@Component
public class ResponseUtils {


    private static MessageSource messageSource;

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        ResponseUtils.messageSource = messageSource;
    }

    public static  <T> ResponseEntity<Data<T>> res(T data){

        return res(data, HttpStatus.OK);
    }

    public static  <T> ResponseEntity<Data<T>> res(T data, HttpStatus status){

        return res(data,"",status);
    }


    public static  <T> ResponseEntity<Data<T>> res(T data, String message){

        return res(data,message, HttpStatus.OK);
    }


    public static  <T> ResponseEntity<Data<T>> res(T data, String message, HttpStatus status){

        return res(data,message,"200",status);
    }

    public static  <T> ResponseEntity<Data<T>> res(T data, String message, String code, HttpStatus status){

        return res(data,message,code,null,status);
    }


    public static  <T> ResponseEntity<Data<T>> res(T data, String message, String code, MultiValueMap<String, String> headers, HttpStatus status){
        return new ResponseEntity(new Data(data,messageSource.getMessage(message,null,message, LocaleContextHolder.getLocale()),code),headers,status);
    }











}
